export async function uploadImg($this) {
  let uploadUrl = process.env.REQUEST_URI + '/v2/file';
  let path = "";
  let token = mpvue.getStorageSync("token");
  if (!token) {
    token = mpvue.getStorageSync("user_token");
  }
  wx.chooseMedia({
    count: 1,
    mediaType: ['image'],
    success(res) {
      let $tmpFilePath = res['tempFiles'][0]['tempFilePath'];
      wx.uploadFile({
        url: uploadUrl,
        filePath: $tmpFilePath,
        name: 'files',
        header: {
          token: token
        },
        success(res) {
          let data = JSON.parse(res.data)
          path = data['data']['path'];
          $this.tmpl_img = path
        }
      })
    }
  })

}

/**
 * toast弹窗
 * @param $state
 * @param $msg
 */
export function showToast($state, $msg) {
  let icon = 'error'
  if ($state) {
    icon = 'success'
  }
  wx.showToast({
    title: $msg,
    icon: icon,
    duration: 1500,
    mask: true
  })
}

/**
 * 处理响应回调
 * @param res
 */
export function doResponse(res) {
  showToast(res.code === 0, res.msg)
  setTimeout(() => {
    wx.navigateBack();
  }, 1500);
}


/**
 * 清空object
 * @param obj
 * @returns {*}
 */
export function clearObjectValues(obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      // 根据属性类型设置空值
      if (typeof obj[key] === 'string') {
        obj[key] = ''; // 清空字符串
      } else if (typeof obj[key] === 'number') {
        obj[key] = 0; // 清空数字
      } else if (Array.isArray(obj[key])) {
        obj[key] = []; // 清空数组
      } else if (typeof obj[key] === 'object' && obj[key] !== null) {
        clearObjectValues(obj[key]); // 递归清空嵌套对象
      } else {
        obj[key] = null; // 其他类型设置为 null
      }
    }
  }
  return obj;
}
